package io.sdl.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.sdl.common.service.impl.BaseServiceImpl;
import io.sdl.common.utils.ConvertUtils;
import io.sdl.common.utils.UploadFileUtils;
import io.sdl.dao.SdlPatientsDao;
import io.sdl.dto.SdlPatientsDTO;
import io.sdl.entity.SdlPatientsEntity;
import cn.hutool.core.util.StrUtil;
import io.sdl.service.SdlPatientsService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 
 *
 * @author Dc 1211734836@qq.com
 * @since 1.0.0 2025-03-28
 */
@Service
@RequiredArgsConstructor
public class SdlPatientsServiceImpl extends BaseServiceImpl<SdlPatientsDao, SdlPatientsEntity> implements SdlPatientsService {

    // 文件存储根路径
    @Value("${file.upload-dir}")
    private String uploadDirResource;

    // 定义常量
    public interface PatientFileFields {
        String REAR_PHOTO = "rearPhoto";
        String BEND_BACK_PHOTO = "bendBackPhoto";
        String BEND_FORWARD_PHOTO = "bendForwardPhoto";
        String LEFT_SIDE_PHOTO = "leftSidePhoto";
        String RIGHT_SIDE_PHOTO = "rightSidePhoto";
        String RIGHT_FRONT_PHOTO = "rightFrontPhoto";
        String CHANGES_OF_BACK = "changesOfBack";
        String X_PHOTO = "XPhoto";
        String TB_PHOTO = "TBPhoto";
    }


    public SdlPatientsServiceImpl(ConversionService conversionService) {
        super();
    }

    @Override
    public List<SdlPatientsDTO> getPatientsByUserId(String phone,Integer page,Integer limit) {

        List<SdlPatientsEntity> patients = baseDao.getPatientsByUserPhone(phone,page-1,limit);

        return ConvertUtils.sourceToTarget(patients, SdlPatientsDTO.class);
    }

    @Override
    public Integer getNum(String phone) {


        return baseDao.getNum(phone);
    }


    @Override
    @Transactional
    public SdlPatientsEntity saveUploadedFile(MultipartFile file, String filedName, Long patientsId) {
        // 1. 参数校验
        if (file.isEmpty()) {
            throw new IllegalArgumentException("上传文件不能为空");
        }
        if (patientsId == null) {
            throw new IllegalArgumentException("患者ID不能为空");
        }
        if (filedName == null || filedName.trim().isEmpty()) {
            throw new IllegalArgumentException("文件字段名不能为空");
        }

        try {
            // 5. 构建访问URL
            String accessUrl = "/uploads/" + UploadFileUtils.uploadFile(file);

            // 6. 更新数据库
            SdlPatientsEntity sdlPatientsEntity = baseDao.selectById(patientsId);
            if (sdlPatientsEntity == null) {
                throw new IllegalArgumentException("找不到ID为" + patientsId + "的患者");
            }

            switch(filedName) {
                case PatientFileFields.REAR_PHOTO:
                    sdlPatientsEntity.setRearPhoto(accessUrl);
                    break;
                case PatientFileFields.BEND_BACK_PHOTO:
                    sdlPatientsEntity.setBendBackPhoto(accessUrl);
                    break;
                case PatientFileFields.BEND_FORWARD_PHOTO:
                    sdlPatientsEntity.setBendForwardPhoto(accessUrl);
                    break;
                case PatientFileFields.LEFT_SIDE_PHOTO:
                    sdlPatientsEntity.setLeftSidePhoto(accessUrl);
                    break;
                case PatientFileFields.RIGHT_SIDE_PHOTO:
                    sdlPatientsEntity.setRightSidePhoto(accessUrl);
                    break;
                case PatientFileFields.RIGHT_FRONT_PHOTO:
                    sdlPatientsEntity.setRightFrontPhoto(accessUrl);
                    break;
                case PatientFileFields.CHANGES_OF_BACK:
                    sdlPatientsEntity.setChangesOfBack(accessUrl);
                    break;
                case PatientFileFields.X_PHOTO:
                    sdlPatientsEntity.setXPhoto(accessUrl);
                    break;
                case PatientFileFields.TB_PHOTO:
                    sdlPatientsEntity.setTbPhoto(accessUrl);
                    break;
                default:
                    throw new IllegalArgumentException("不支持的文件字段: " + filedName);
            }

            baseDao.updateById(sdlPatientsEntity);
            return sdlPatientsEntity;
        } catch (IOException e) {
            throw new RuntimeException("文件存储失败", e);
        }
    }

    @Override
    public List<SdlPatientsEntity> getByIdCard(String idCard) {

        return baseDao.getByIdCard(idCard);
    }

}